**This do-file extracts election dates from the country*date version of V-Dem 

** Parliamentary elections

use country_name year historical_date v2eltype_0 v2eltype_1 v2eltype_4 v2eltype_5 v2eltype_6 v2eltype_7 v2xel_elecpres v2xel_elecparl using  "$project_path/data/1_input/elections/V-Dem/vdem_cd_extract.dta", clear

keep if v2xel_elecparl==1
keep if year>1945
keep if year<=2018

gen round = 1 if v2eltype_0==1 | v2eltype_4==1
replace round = 2 if v2eltype_1==1 | v2eltype_5==1

keep country_name year historical_date round

// For each election round, we generate a variable which indicates whether the next election round was a first or second round.
sort country_name historical_date
bysort country_name : gen next_round = round[_n+1]

// We drop first rounds if they are followed by a second round
drop if round==1 & next_round==2

keep country_name year historical_date round

// When the coders of V-Dem did not know the date of an election, they indicated that the election took place on Jan, 1. We remove such cases.
gen month = month(historical_date)
drop if month==1 & day(historical_date)==1

// We indicate the month only if there are several elections of the same type on a given year
bys country_name year : gen temp=_n
bys country_name year : egen maxtemp=max(temp)
replace month = . if maxtemp==1
drop temp maxtemp

// Sometimes, we have several elections for a country*year*month. This usually corresponds to a first/second round which has not been coded properly. When we encounter such a case, we only keep the second observation.
sort country_name year month historical_date
by country_name year month : gen temp = _n
by country_name year month : egen maxtemp = max(temp)
drop if temp!=maxtemp

keep country_name year month historical_date 

// We standardize country names
replace country_name="Occupied Palestinian Territories"  if country_name=="Palestine/West Bank"
replace country_name="Bosnia-Herz"  if country_name=="Bosnia and Herzegovina"
replace country_name="Myanmar"  if country_name=="Burma/Myanmar"
replace country_name="C. Verde Is."  if country_name=="Cape Verde"
replace country_name="Cent. Af. Rep."  if country_name=="Central African Republic"
replace country_name="PRC"  if country_name=="China"
replace country_name="Comoro Is."  if country_name=="Comoros"
replace country_name="Czech Rep."  if country_name=="Czech Republic"
replace country_name="Congo (DRC)"  if country_name=="Democratic Republic of the Congo"
replace country_name="Dom. Rep."  if country_name=="Dominican Republic"
replace country_name="Eq. Guinea"  if country_name=="Equatorial Guinea"
replace country_name="Swaziland"  if country_name=="Eswatini"
replace country_name="GDR"  if country_name=="German Democratic Republic"
replace country_name="FRG/Germany"  if country_name=="Germany"
replace country_name="PRK"  if country_name=="North Korea"
replace country_name="Macedonia"  if country_name=="North Macedonia"
replace country_name="P. N. Guinea"  if country_name=="Papua New Guinea"
replace country_name="Congo"  if country_name=="Republic of the Congo"
replace country_name="Solomon Is."  if country_name=="Solomon Islands"
replace country_name="S. Africa"  if country_name=="South Africa"
replace country_name="Korea, Republic of"  if country_name=="South Korea"
replace country_name="Gambia"  if country_name=="The Gambia"
replace country_name="Trinidad-Tobago"  if country_name=="Trinidad and Tobago"
replace country_name="UAE"  if country_name=="United Arab Emirates"
replace country_name="UK"  if country_name=="United Kingdom"
replace country_name="USA"  if country_name=="United States of America"
drop if country_name=="Hong Kong"
drop if country_name=="Republic of Vietnam"
drop if country_name=="Zanzibar"

ren historical_date Date
ren country_name Country
ren year Year 
ren month Month 
gen Type_Election = "Parliamentary"

tempfile parl
save `parl'

** Presidential elections

use country_name year historical_date v2eltype_0 v2eltype_1 v2eltype_4 v2eltype_5 v2eltype_6 v2eltype_7 v2xel_elecpres v2xel_elecparl using  "$project_path/data/1_input/elections/V-Dem/vdem_cd_extract.dta", clear

keep if v2xel_elecpres==1
keep if year>1945
keep if year<=2018

// We generate a variable which gives for each election date whether this was a first or second round
gen round=1 if v2eltype_6==1
replace  round=2 if v2eltype_7==1

keep country_name year historical_date round

// For each election round, we generate a variable which indicates whether the next election round was a first or second round.
sort country_name historical_date
bysort country_name : gen next_round = round[_n+1]

// We drop first rounds if they are followed by a second round
drop if round==1 & next_round==2

keep country_name year historical_date round

// When the coders of V-Dem did not know the date of an election, they indicated that the election took place on Jan, 1. We remove such cases
gen month = month(historical_date)
drop if month==1 & day(historical_date)==1

// We indicate the month only if there are several elections of the same type on a given year
sort country_name year month historical_date
bys country_name year : gen temp=_n
bys country_name year : egen maxtemp=max(temp)
replace month=. if maxtemp==1
drop temp maxtemp

*Sometimes, we have several elections for a country*year*month. This usually corresponds to a first/second round
*which has not been coded properly. When we encounter such a case, we only keep the second observation.
bys country_name year month : gen temp = _n
bys country_name year month : egen maxtemp = max(temp)
drop if temp!=maxtemp

keep country_name year month historical_date 

// We standardize country names
replace country_name="Bosnia-Herz"  if country_name=="Bosnia and Herzegovina"
replace country_name="C. Verde Is."  if country_name=="Cape Verde"
replace country_name="Cent. Af. Rep."  if country_name=="Central African Republic"
replace country_name="Comoro Is."  if country_name=="Comoros"
replace country_name="Czech Rep."  if country_name=="Czech Republic"
replace country_name="Congo (DRC)"  if country_name=="Democratic Republic of the Congo"
replace country_name="Dom. Rep."  if country_name=="Dominican Republic"
replace country_name="Eq. Guinea"  if country_name=="Equatorial Guinea"
replace country_name="Occupied Palestinian Territories"  if country_name=="Palestine/West Bank"
replace country_name="Congo"  if country_name=="Republic of the Congo"
replace country_name="Korea, Republic of"  if country_name=="South Korea"
replace country_name="Sao Tome and Principe"  if country_name=="São Tomé and Príncipe"
replace country_name="Serbia and Montenegro"  if country_name=="Serbia" & year==2000
replace country_name="Gambia"  if country_name=="The Gambia"
replace country_name="USA"  if country_name=="United States of America"
drop if country_name=="Republic of Vietnam"
drop if country_name=="Zanzibar"

ren historical_date Date
ren country_name Country
ren year Year 
ren month Month 
gen Type_Election="Presidential"

append using `parl'

sort Type_Election Country Year Month Date
order Country Year Month Type_Election Date
format Country %20s
compress

save "$project_path/data/2_intermediary/elections/Election dates/dates_vdem.dta", replace
